<?php
namespace app\common\taglib;

use think\template\TagLib;

class Sys extends TagLib
{
    protected $tags = [
        'load'      => ['attr' => 'file,href', 'close' => 0],
        'loader'    => ['attr' => 'href', 'close' => 0],
        'editor'    => ['attr' => 'id,default,toolbar', 'close' => 0],
		'image'		=> ['attr' => 'id,default,dis', 'close' => 0],
        'img'		=> ['attr' => 'id,default,dis', 'close' => 0]
    ];

    /**
     * 用法{sys:load file='file.php'}
     * {sys:load href='a.css,b.js,c.jpg'}
     * @param $tag
     * @return string
     */
    public function tagLoad($tag)
    {
        if ( isset($tag['file']) ) {
            $file = $tag['file'] . '.php';
            return "<?php include __DIR__ . '".$file."'; ?>";
        }
        if ( isset($tag['href']) ) {
            return $this->loadStatic($tag['href'], '/public/static/');
        }
        return '';
    }

    /**
     * 用法{sys:loader href='a.css,b.js'}
     * @param $tag
     * @return string
     */
    public function tagLoader($tag)
    {
        return $this->loadStatic($tag['href'], '/template/default/');
    }

    /**
     * @param $files
     * @param $dir
     */
    private function loadStatic($files, $dir)
    {
        $html = '';
        $href = explode(',', $files );
        $count = count( $href );
        if ( $count > 0 ) {
            for ($i = 0; $i < $count; $i++) {
                if ( stripos($href[$i] , '.js') !== false ) {
                    $html .= "<script type=\"text/javascript\" src=\"". $dir .
                        $href[$i] ."?time=".date('YmdHi')."\"></script>\n";
                } else if ( stripos($href[$i], '.css') !== false ) {
                    $html .= '<link href="' . $dir . $href[$i] . '?time='.
                        date('YmdHi').'" rel="stylesheet">'."\n";
                } else {
                    $html .= $dir.$href[$i].'?time='.date('YmdHi');
                }
            }
        }
        return $html;
    }

    /**
     * [tagEditor 编辑器标签]
	 * 用法 {sys:editor id="content" default="hello world"}
     * @param  [type] $tag [description]
     * @return [type]      [description]
     */
    public function tagEditor($tag)
    {
        $id = isset( $tag['id'] ) ? $tag['id'] : 'content';
        $default = isset( $tag['default'] ) ? $tag['default'] : '';
		$toolbar = '';
		if ( isset($tag['toolbar']) ) {
			$tag['toolbar'] = explode(',', $tag['toolbar']);
			foreach( $tag['toolbar'] as $tool) {
				$toolbar .= empty($toolbar) ? "'".$tool."'" : ",'".$tool."'";
			}
		}
		
        $html = "<textarea id='{$id}' name='{$id}' style='width: 100%;min-height: 300px'>{$default}</textarea>";
        $html .= "<script type='text/javascript'>";
        $html .= "UE.delEditor('container');";
        $html .= "var ue = UE.getEditor('{$id}',{";
        $html .= "    autoHeight : true,";
        $html .= "   serverUrl: \"/admin/index/uploaded\"";
		if ( !empty($toolbar) ) {
			$html .= "  , toolbars: [[{$toolbar}]]";
		}
        $html .= "});";
        $html .= "</script>";
        return $html;
    }
	
	/**
	 * 用法{sys:image id="image" default=""}
	 * @param {Object} $tag
	 */
    public function tagImage($tag)
    {
        $id = isset( $tag['id'] ) ? $tag['id'] : 'image';
		$default = isset( $tag['default'] ) ? $tag['default'] : '';
		$dis = isset( $tag['dis'] ) ? $tag['dis'] : 'image';
		
		$html = <<<EOF
			<input type="text" class="form-control" name="{$id}" id="{$id}" value="{$default}" />
			<div class="input-group-btn"><button class="btn btn-default" type="button" id="btn-{$id}">上传图片</button></div>
			<script type="text/javascript">
				layui.use(['upload','layer', 'jquery'], function(){
					let upload = layui.upload,
						layer = layui.layer,
						$ = layui.jquery;
						
					var uploadIndex{$id} = upload.render({
						elem: '#btn-{$id}',
						url: '/admin/index/uploader?dis={$dis}',
						done: function( response , index, upload ) {
						    if ( response.state == 'success' ) {
						        $("#{$id}").val(response.data.url);
						        layer.msg('上传成功!')
						    } else {
						        layer.msg( response.message );
						    }
						}
					})
				})
			</script>
EOF;
		return $html;
    }

    /**
     * 用法{sys:image id="image" default=""}
     * @param {Object} $tag
     */
    public function tagImg($tag)
    {
        $id = isset( $tag['id'] ) ? $tag['id'] : 'image';
        $default = isset( $tag['default'] ) ? $tag['default'] : '';
        $dis = isset( $tag['dis'] ) ? $tag['dis'] : 'image';

        $html = <<<EOF
			<input type="text" class="form-control" name="{$id}" id="{$id}" value="{$default}" />
			<div class="input-group-btn"><button class="btn btn-default" type="button" id="btn-{$id}">上传图片</button></div>
			<script type="text/javascript">
				layui.use(['upload','layer', 'jquery'], function(){
					let upload = layui.upload,
						layer = layui.layer,
						$ = layui.jquery;
						
					var uploadIndex{$id} = upload.render({
						elem: '#btn-{$id}',
						url: '/admin/index/uploader?dis={$dis}',
						done: function( response , index, upload ) {
						    if ( response.state == 'success' ) {
						        $("#{$id}").val(response.data.path);
						        layer.msg('上传成功!')
						    } else {
						        layer.msg( response.message );
						    }
						}
					})
				})
			</script>
EOF;
        return $html;
    }
}
